//
//  ZhangBenPageViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/10/12.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "ZhangBenPageViewController.h"
#import "JTCSM.h"
#import "SeePlanModel.h"
#import "SeePlanTableViewCell.h"
#import "TheTypeView.h"

@interface ZhangBenPageViewController ()<UITableViewDelegate,UITableViewDataSource,TheTypeViewDelegate>
{
    UITableView *_tableview;
    NSMutableArray *titlearray;
    UIView *backView;
    NSString *type;
    NSString *page;
    UIButton *button;
    TheTypeView *typeview;
}


@end

@implementation ZhangBenPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    type = @"0";
    page = @"";
    
    [self.myItemlabel removeFromSuperview];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 60, 44);
    [button setTitle:@"全部" forState:UIControlStateNormal];
    [button setTitleColor:RGBACOLOR(89, 89, 89, 1) forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"xiangxia@2x.png"] forState:UIControlStateNormal];
    CGFloat imagesize = 30;
    //        title的位置大小调整
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -40, 0, 20);
    //        image的位置大小调整
    button.imageEdgeInsets = UIEdgeInsetsMake((button.frame.size.height-imagesize)/2, (button.frame.size.width-imagesize) ,(button.frame.size.height-imagesize)/2 , 0);
    
    [button addTarget:self action:@selector(gotobackVC) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setTitleView:button];
    
    [self layoutTableView];
}

- (void)changeButtonSizeWithtitle:(NSString *)title{
    
    if (title.length==2) {
        button.frame = CGRectMake(0, 0, 60, 44);
    }else if(title.length ==3){
        button.frame = CGRectMake(0, 0, 72, 44);
    }else{
        button.frame = CGRectMake(0, 0, 75, 44);
    }
    
    [button setTitle:title forState:UIControlStateNormal];
    CGFloat imagesize = 30;
    //        title的位置大小调整
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -(button.frame.size.width-20), 0, 20);
    //        image的位置大小调整
    button.imageEdgeInsets = UIEdgeInsetsMake((button.frame.size.height-imagesize)/2, (button.frame.size.width-imagesize) ,(button.frame.size.height-imagesize)/2 , 0);
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self theRequest];

}
- (void)gotobackVC{
    typeview = [[TheTypeView alloc]init];
    typeview.delegate = self;
    [[JTCSMSingle shareSingle].theWindow addSubview:typeview];

}

- (void)getTheTitle:(NSString *)title type:(NSString *)mytype{
    [self changeButtonSizeWithtitle:title];
    type = mytype;
    [self theRequest];
}

- (void)layoutTableView{
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, KCONTROLLERHeight-50)];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableview.backgroundColor = [UIColor whiteColor];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //
    return titlearray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellid = [NSString stringWithFormat:@"iii%ld",(long)indexPath.row];
    
    SeePlanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[SeePlanTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid SeePlanTableViewCellType:isZhangDanType];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (indexPath == nil || indexPath.row>=titlearray.count) {
        return cell;
    }
    [cell setvalueWithSeePlanModel:titlearray[indexPath.row] SeePlanTableViewCellType:isZhangDanType];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SeePlanTableViewCell whc_CellHeightForIndexPath:indexPath tableView:tableView];
}

- (void)theRequest{
    [titlearray removeAllObjects];
    titlearray = nil;
    titlearray = [NSMutableArray array];
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"UserBill/getList") withDict:@{@"token":KTokenString,@"type":type} superView:nil isAddtag:NO Block:^(id result) {
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            NSArray *bannerArr = [KResultOfKey(@"data") objectForKey:@"list"];
            [titlearray removeAllObjects];
            for (NSDictionary *bannerdict in bannerArr) {
                SeePlanModel *model = [SeePlanModel whc_ModelWithJson:bannerdict];
                [titlearray addObject:model];
            }
            
            if (titlearray == nil || titlearray.count == 0) {
                [[MyAlertView shareSingle] showWithImagename:@"bonusimage.png" contentLabel:@"暂无账单" superview:_tableview];
            }else{
                [[MyAlertView shareSingle] hiddenmyself];
            }
            
            [_tableview reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
